package com.yantong.controller;

import com.yantong.pojo.CentreTerminal;
import com.yantong.service.CentreTerminalService;
import com.yantong.annotation.Log;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.utils.ValidateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 商家门店终端表控制类
 *
 * @author huangshuai-728982210@qq.com
 * @date 2021/1/16 15:15
 */

@Controller
@RequestMapping("/centre/centreTerminal")
public class CentreTerminalController extends BaseController {
	@Autowired
	private CentreTerminalService centreTerminalService;
	private static final String prefix = "centre/centreTerminal";



	/**
	 * 商家门店终端列表页面
	 *
	 * @return
	 */
	@GetMapping()
	@RequiresPermissions("centre:centreTerminal:centreTerminal")
	public String centreTerminal(Model model) {
		return prefix + "/centreTerminal";
	}

	/**
	 * 商家门店终端列表数据
	 *
	 * @param centreTerminal
	 * @param offset
	 * @param limit
	 * @return
	 */
	@GetMapping("/list")
	@RequiresPermissions("centre:centreTerminal:centreTerminal")
	@ResponseBody
	public PageUtils<CentreTerminal> listCentreTerminal(CentreTerminal centreTerminal, int offset, int limit) {
		centreTerminal.setCentreId(getUserCentreId());
		return centreTerminalService.listCentreTerminal(centreTerminal, offset, limit);
	}

	/**
	 * 商家门店终端编辑页面
	 * @param centreTerminalId
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{centreTerminalId}")
	@RequiresPermissions("centre:centreTerminal:edit")
	public String edit(@PathVariable Long centreTerminalId, Model model) {
		model.addAttribute("centreTerminal", centreTerminalService.getCentreTerminalById(centreTerminalId));
		return prefix + "/edit";
	}

	/**
	 * 商家门店终端编辑操作
	 * @param centreTerminal
	 * @return
	 */
	@Log(module = "商家门店终端管理", operation = "编辑商家门店终端")
	@ResponseBody
	@RequiresPermissions("centre:centreTerminal:edit")
	@PostMapping("/update")
	public ResultVo update(CentreTerminal centreTerminal) {
		if (centreTerminal == null || centreTerminal.getId() == null) {
			return ResultVo.error();
		}
		if (centreTerminalService.updateCentreTerminal(centreTerminal) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 商家门店终端移除操作
	 * @param id
	 * @return
	 */
	@Log(module = "商家门店终端管理", operation = "移除商家门店终端")
	@ResponseBody
	@RequiresPermissions("centre:centreTerminal:remove")
	@PostMapping("/remove")
	public ResultVo remove(Long id) {
		if (ValidateUtil.isEmptyObjectOrString(id)) {
			return ResultVo.error();
		}
		if (centreTerminalService.removeCentreTerminal(id) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	@RequiresPermissions("centre:centreTerminal:add")
	@GetMapping("/add")
	public String add(Model model) {
		return prefix +"/add";
	}


	@Log(module = "商家门店终端管理", operation = "添加商家门店终端")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("centre:centreTerminal:add")
	public ResultVo saveRate(CentreTerminal centreTerminal) {
		if (centreTerminal == null) {
			return ResultVo.error();
		}
		centreTerminal.setCentreId(getUserCentreId());
		if (centreTerminalService.saveCentreTerminal(centreTerminal) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}


}
